package com.ruoyi.mining.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.ip.IpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.mining.domain.Paper;
import com.ruoyi.mining.service.IPaperService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

import static com.ruoyi.common.core.web.page.TableSupport.getPageDomain;

@Api(value = "试卷", tags = "试卷Controller")
@RestController
@RequestMapping("/paper")
public class PaperController extends BaseController {
    @Autowired
    private IPaperService paperService;

    /**
     * 查询试卷列表
     */
//    @RequiresPermissions("mining:paper:list")
    @GetMapping("/list")
    public TableDataInfo list(Paper paper) {
        startPage();
        List<Paper> list = paperService.selectPaperList(paper);
        return getDataTable(list);
    }

    /**
     * 导出试卷列表
     */
//    @RequiresPermissions("mining:paper:export")
/*    @Log(title = "试卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Paper paper) {
        List<Paper> list = paperService.selectPaperList(paper);
        ExcelUtil<Paper> util = new ExcelUtil<Paper>(Paper.class);
        util.exportExcel(response, list, "试卷数据");
    }*/

    /**
     * 获取试卷详细信息
     */
//    @RequiresPermissions("mining:paper:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(paperService.selectPaperById(id));
    }

    /**
     * 新增试卷
     */
//    @RequiresPermissions("mining:paper:add")
//    @Log(title = "试卷", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody Paper paper) {
//        return toAjax(paperService.insertPaper(paper));
//    }

    /**
     * 修改试卷
     */
//    @RequiresPermissions("mining:paper:edit")
    @Log(title = "试卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Paper paper) {
        return toAjax(paperService.updatePaper(paper));
    }



    @ApiOperation("下载次数")
    @GetMapping("/downloadById")
    public AjaxResult downloadById(@ApiParam(value = "下载次数id", required = true) @RequestParam(value = "id") String id) {

        String ip = IpUtils.getIpAddr();
        System.out.println("ip地址 +" + ip);
        return success(paperService.downloadById(id));
    }


    /**
     * 删除试卷
     */
//    @RequiresPermissions("mining:paper:remove")
    @Log(title = "试卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(paperService.deletePaperByIds(ids));
    }
}
